﻿using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;

namespace ABC.HRMS
{
    public partial class Main : System.Web.UI.Page
    {
        private IList<Menu> menuList = new List<Menu>()
        {
            new Menu(){ ID = 1, Title = "系统管理", Url ="", ImgUrl = "Image/frame/sys-menu.png", ParentID = 0, Order = 0},
            new Menu(){ ID = 2, Title = "用户管理", Url ="Html/UserList.htm", ImgUrl = "", ParentID = 1, Order = 0},
            new Menu(){ ID = 3, Title = "角色管理", Url ="", ImgUrl = "", ParentID = 1, Order = 1},
            new Menu(){ ID = 4, Title = "组织机构管理", Url ="", ImgUrl = "", ParentID = 1, Order = 2},
            new Menu(){ ID = 5, Title = "档案管理", Url ="", ImgUrl = "", ParentID = 0, Order = 1},
            new Menu(){ ID = 6, Title = "档案中心", Url ="", ImgUrl = "", ParentID = 5, Order = 0},
            new Menu(){ ID = 7, Title = "档案统计", Url ="", ImgUrl = "", ParentID = 5, Order = 1},
            new Menu(){ ID = 8, Title = "人员管理", Url ="", ImgUrl = "", ParentID = 0, Order = 2},
            new Menu(){ ID = 9, Title = "考勤管理", Url ="", ImgUrl = "", ParentID = 0, Order = 3},
            new Menu(){ ID = 10, Title = "报表中心", Url ="", ImgUrl = "", ParentID = 0, Order = 4}
        };

        protected void Page_Load(object sender, EventArgs e)
        {
            //string action = Request.Form["action"];
            //if (action == null) return;

            var temp = (from m in menuList where m.ParentID == 0 orderby m.Order select m);
            if (temp == null)
            {
                Response.Write("");
                return;
            }
            StringBuilder htmlBuffer = new StringBuilder();
            IList<Menu> rootMenuList = temp.ToList<Menu>();
            foreach (Menu item in rootMenuList)
            {
                htmlBuffer.Append("<h1><img alt='' src='");
                htmlBuffer.Append(item.ImgUrl);
                htmlBuffer.Append("' />");
                htmlBuffer.Append(item.Title);
                htmlBuffer.Append("</h1>");
                var tempChild = (from m in menuList where m.ParentID == item.ID orderby m.Order select m);
                if (tempChild == null || tempChild.Count<Menu>() < 1)
                {
                    htmlBuffer.Append("<div></div>");
                    continue;
                }
                htmlBuffer.Append("<div><ul>");
                htmlBuffer.Append(CreateChildrenHtml(tempChild.ToList<Menu>()));
                htmlBuffer.Append("</ul></div>");
            }

            ltMenu.Text = htmlBuffer.ToString();
        }

        private string CreateChildrenHtml(IList<Menu> menuList)
        {
            StringBuilder childBuffer = new StringBuilder();
            for (int i = 0; i < menuList.Count; i++)
            {
                childBuffer.Append("<li title='");
                childBuffer.Append(menuList[i].Url);
                childBuffer.Append("'><img alt='' src='");
                childBuffer.Append(menuList[i].ImgUrl);
                childBuffer.Append("' />");
                childBuffer.Append(menuList[i].Title);
                childBuffer.Append("</li>");
                if (i != menuList.Count - 1)
                {
                    childBuffer.Append("<hr />");
                }
            }

            return childBuffer.ToString();
        }
    }

    public class Menu
    {
        public virtual long ID       { get; set; }
        public virtual string Title  { get; set; }
        public virtual string Url    { get; set; }
        public virtual string ImgUrl { get; set; }
        public virtual long ParentID { get; set; }
        public virtual long Order    { get; set; }
    }
}